package com.lyy.mybatis.sensitive.demo.controller;


import cn.hutool.core.bean.BeanUtil;
import com.lyy.mybatis.sensitive.demo.domain.entity.UserEntity;
import com.lyy.mybatis.sensitive.demo.domain.dto.Other;
import com.lyy.mybatis.sensitive.demo.domain.res.UserRes;
import com.lyy.mybatis.sensitive.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author lyy 2024/11/27
 */
@RestController
@RequestMapping("/test")
public class TestController {
    @Autowired
    UserService userService;

    @PostMapping("/save")
    public void save(@RequestParam("mobile") String mobile, @RequestParam("addr") String addr) {
        userService.getBaseMapper().insert(new UserEntity().setMobile(mobile).setAddr(addr));
    }

    @PostMapping("/saveWithOther")
    public void saveWithOther(@RequestBody UserEntity userEntity) {
        userService.getBaseMapper().insert(userEntity);
    }

    @GetMapping("/list")
    public List<UserRes> list(@RequestParam(value = "mobile", required = false) String mobile,
                              @RequestParam(value = "addr", required = false) String addr) {
        List<UserEntity> list = userService
                .lambdaQuery()
                .eq(mobile != null, UserEntity::getMobile, mobile)
                .eq(addr != null, UserEntity::getAddr, addr)
                .list();
        return BeanUtil.copyToList(list, UserRes.class);
    }
}
